<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: categories.php 24 2013-09-21 11:53:41Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 24 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:53:41 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once('includes/application_top.php');
require_once('includes/classes/'.FILENAME_IMAGEMANIPULATOR);
require_once('includes/classes/class.categories.php');
require_once(DIR_FS_INC.'inc.get_tax_rate.php');
require(DIR_FS_INC.'inc.get_products_mo_images.php');
require_once(DIR_WS_CLASSES.'currencies.php');

if(MODULE_SEO_URL_INDEX_STATUS == 'True') {
	require_once(DIR_FS_INC.'inc.seo_url.php');
	!$seo_url ? $seo_url = new SeoUrl() : false;
}

$currencies = new currencies($db);
$catfunc = new categories($db);

if($_GET['function']) {
	switch ($_GET['function']) {
		case 'delete' :
			$db->db_query("DELETE FROM
								".TABLE_PERSONAL_OFFERS_BY.(int) $_GET['statusID']."
							WHERE
								products_id = '".(int)$_GET['pID']."'
							AND
								quantity = '".(int)$_GET['quantity']."'");
			break;
	}
	redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&action=new_product&pID='.(int) $_GET['pID'].get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
}

if(isset($_POST['multi_status_on'])) {
	//set multi_categories status=on
	if (is_array($_POST['multi_categories'])) {
		foreach ($_POST['multi_categories'] AS $key => $category_id) {
			$catfunc->set_category_recursive($category_id, '1');
		}
	}
	//set multi_products status=on
	if(is_array($_POST['multi_products'])) {
		foreach ($_POST['multi_products'] AS $key => $product_id) {
			$catfunc->set_product_status($product_id, '1');
		}
	}
	redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&'.get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
}

if (isset ($_POST['multi_status_off'])) {
	//set multi_categories status=off
	if (is_array($_POST['multi_categories'])) {
		foreach ($_POST['multi_categories'] AS $key => $category_id) {
			$catfunc->set_category_recursive($category_id, "0");
		}
	}
	//set multi_products status=off
	if (is_array($_POST['multi_products'])) {
		foreach ($_POST['multi_products'] AS $key => $product_id) {
			$catfunc->set_product_status($product_id, "0");
		}
	}
	redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&'.get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
}

if($_GET['action']) {
	switch ($_GET['action']) {
		case 'setcflag' :
			if (($_GET['flag'] == '0') || ($_GET['flag'] == '1')) {
				if ($_GET['cID'])
					$catfunc->set_category_recursive($_GET['cID'], $_GET['flag']);
			}
			redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&cID='.$_GET['cID'].get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			break;

		case 'setcpflag' :
			if (($_GET['flag'] == '0') || ($_GET['flag'] == '1')) {
				if ($_GET['cID'])
					$catfunc->set_category_product_recursive($_GET['cID'], $_GET['flag']);
			}
			redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&cID='.$_GET['cID'].get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			break;

		case 'setpflag' :
			if (($_GET['flag'] == '0') || ($_GET['flag'] == '1')) {
				if ($_GET['pID'])
					$catfunc->set_product_status($_GET['pID'], $_GET['flag']);
			}
			if ($_GET['pID'])
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&pID='.$_GET['pID'].get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			else
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&cID='.$_GET['cID'].get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			break;

		case 'setsflag' :
			if (($_GET['flag'] == '0') || ($_GET['flag'] == '1')) {
				if ($_GET['pID']) {
					$catfunc->set_product_startpage($_GET['pID'], $_GET['flag']);
					if ($_GET['flag'] == '1') 
						$catfunc->link_product($_GET['pID'], 0);
				}
			}
			if ($_GET['pID'])
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&pID='.$_GET['pID'].get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			else
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&cID='.$_GET['cID'].get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			break;

		case 'unlink_startpage' :
			if(!empty($_GET['products_id'])) {
				$db->db_query("DELETE FROM ".TABLE_PRODUCTS_TO_CATEGORIES." WHERE products_id = '".(int)$_GET['products_id']."' AND categories_id = '0' ");
				$db->db_query("UPDATE products SET products_startpage = '0', products_startpage_sort = '0' WHERE products_id = '".(int)$_GET['products_id']."' ");
				redirect(href_link(FILENAME_CATEGORIES, 'cPath=0'));
			}
			redirect(href_link(FILENAME_CATEGORIES, 'cPath=0'));
			break;

		case 'update_category' :
			$cID = $catfunc->insert_category($_POST, '', 'update');
			if(MODULE_SEO_URL_INDEX_STATUS == 'True')
				$seo_url->updateSeoDBTable('category', $cID);
			if(isset($_POST['apply_new_category']))
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&action=edit_category&cID='.$_POST['categories_id'].get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			break;

		case 'insert_category' :
			$cID = $catfunc->insert_category($_POST, $current_category_id);
			if(MODULE_SEO_URL_INDEX_STATUS == 'True')
				$seo_url->insertSeoDBTable('category', $cID);
			if(isset($_POST['save_new_category_and_more']))
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&action=new_category'.get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			break;

		case 'update_product' :
			$pID = $catfunc->insert_product($_POST, '', 'update');
			if(MODULE_SEO_URL_INDEX_STATUS == 'True')
				$seo_url->updateSeoDBTable('product', $pID);
			if(isset($_POST['apply_new_product_mi']))
				redirect(href_link(FILENAME_CATEGORIES, get_all_get_params(array('cPath', 'action', 'pID', 'cID', 'menu')).'cPath='.$_POST['cPath'][0].'&action=new_product&pID='.$_POST['products_id'].'#'.$_POST['selected_tab']));
			elseif(isset($_POST['save_new_product_and_more']))
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_POST['cPath'][0].'&action=new_product'.get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			break;

		case 'insert_product' :
			$pID = $catfunc->insert_product($_POST, $_POST['cPath']);
			if(MODULE_SEO_URL_INDEX_STATUS == 'True')
				$seo_url->insertSeoDBTable('product', $pID);
			if(isset($_POST['save_new_product_and_more']))
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_POST['cPath'][0].'&action=new_product'.get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			elseif(isset($_POST['apply_new_product_oi']))
				redirect(href_link(FILENAME_CATEGORIES, get_all_get_params(array('cPath', 'action', 'pID', 'cID', 'menu')).'cPath='.$_POST['cPath'][0].'&action=new_product&pID='.$pID.'#'.$_POST['selected_tab']));
			elseif(isset($_POST['apply_new_product']))
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_POST['cPath'][0].'&action=new_product&pID='.$_POST['products_id'].get_all_get_params(array('cPath', 'action', 'pID', 'cID', 'menu')).'#'.$_POST['selected_tab']));
			break;

		case 'edit_crossselling' :
			$catfunc->edit_cross_sell($_GET);
			break;

		case 'multi_action_confirm' :
			if(isset($_POST['multi_delete_confirm'])) {
				if (is_array($_POST['multi_categories'])) {
					foreach($_POST['multi_categories'] AS $category_id) {
						$catfunc->remove_categories($category_id);
						if(MODULE_SEO_URL_INDEX_STATUS == 'True')
							$seo_url->deleteFromDB('categories_id', $category_id);
					}
					$message_stack->add_session(TEXT_CATEGORIES_DELETED, 'success');
				}
				
				if(is_array($_POST['multi_products'])) {
					foreach ($_POST['multi_products'] AS $product_id) {
						$catfunc->delete_product($product_id);
						if(MODULE_SEO_URL_INDEX_STATUS == 'True')
							$seo_url->deleteFromDB('products_id', $product_id);
					}
					$message_stack->add_session(TEXT_PRODUCTS_DELETED, 'success');
				}
			}

			if(isset($_POST['multi_move_confirm'])) {
				if (is_array($_POST['multi_categories']) && not_null($_POST['move_to_category_id'])) {
					foreach ($_POST['multi_categories'] AS $category_id) {
						$dest_category_id = $_POST['move_to_category_id'];
						if ($category_id != $dest_category_id)
							$catfunc->move_category($category_id, $dest_category_id);
						if(MODULE_SEO_URL_INDEX_STATUS == 'True')
							$seo_url->updateSeoDBTable('category', $category_id);
					}
				}

				if (is_array($_POST['multi_products']) && not_null($_POST['move_to_category_id']) && not_null($_POST['src_category_id'])) {
					foreach ($_POST['multi_products'] AS $product_id) {
						$product_id = $product_id;
						$src_category_id = $_POST['src_category_id'];
						$dest_category_id = $_POST['move_to_category_id'];
						$catfunc->move_product($product_id, $src_category_id, $dest_category_id);
						if(MODULE_SEO_URL_INDEX_STATUS == 'True')
							$seo_url->updateSeoDBTable('product', $product_id);
					}
				}
				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$dest_category_id.'&'.get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			}

			// --- MULTI COPY ---
			if (isset($_POST['multi_copy_confirm'])) {
				if (is_array($_POST['multi_categories']) && (is_array($_POST['dest_cat_ids']) || not_null($_POST['dest_category_id']))) {
					$_SESSION['copied'] = array();
					foreach ($_POST['multi_categories'] AS $category_id) {
						if (is_array($_POST['dest_cat_ids'])) {
							foreach ($_POST['dest_cat_ids'] AS $dest_category_id) {
								if ($_POST['copy_as'] == 'link')
									$catfunc->copy_category($category_id, $dest_category_id, 'link');
								elseif ($_POST['copy_as'] == 'duplicate')
									$catfunc->copy_category($category_id, $dest_category_id, 'duplicate');
								else
									$message_stack->add_session(TEXT_COPY_TYPE_ERROR, 'error');
							}
							
						} elseif (not_null($_POST['dest_category_id'])) {
							if ($_POST['copy_as'] == 'link')
								$catfunc->copy_category($category_id, $_POST['dest_category_id'], 'link');
							elseif ($_POST['copy_as'] == 'duplicate')
								$catfunc->copy_category($category_id, $_POST['dest_category_id'], 'duplicate');
							else
								$message_stack->add_session(TEXT_COPY_TYPE_ERROR, 'error');
						}
						if(MODULE_SEO_URL_INDEX_STATUS == 'True')
							$seo_url->insertSeoDBTable('category', $category_id);
					}
					unset($_SESSION['copied']);
				}
				if (is_array($_POST['multi_products']) && (is_array($_POST['dest_cat_ids']) || not_null($_POST['dest_category_id']))) {
					foreach ($_POST['multi_products'] AS $product_id) {
						if (is_array($_POST['dest_cat_ids'])) {
							foreach ($_POST['dest_cat_ids'] AS $dest_category_id) {
								if ($_POST['copy_as'] == 'link')
									$catfunc->link_product($product_id, $dest_category_id);
								elseif ($_POST['copy_as'] == 'duplicate')
									$catfunc->duplicate_product($product_id, $dest_category_id);
								else
									$message_stack->add_session(TEXT_COPY_TYPE_ERROR, 'error');
								if(MODULE_SEO_URL_INDEX_STATUS == 'True')
									$seo_url->updateSeoDBTable('product', $product_id);
							}
							
						} elseif (not_null($_POST['dest_category_id'])) {
							$dest_category_id = $_POST['dest_category_id'];
							if ($_POST['copy_as'] == 'link')
								$catfunc->link_product($product_id, $dest_category_id);
							elseif ($_POST['copy_as'] == 'duplicate')
								$catfunc->duplicate_product($product_id, $dest_category_id);
							else
								$message_stack->add_session(TEXT_COPY_TYPE_ERROR, 'error');
							if(MODULE_SEO_URL_INDEX_STATUS == 'True')
								$seo_url->insertSeoDBTable('product', $product_id);
						}
					}
				}

				redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$dest_category_id.'&'.get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			}

			redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&'.get_all_get_params(array ('cPath', 'action', 'pID', 'cID', 'menu'))));
			break;
	}

	if($_GET['action'] == 'update_category' || $_GET['action'] == 'insert_category' || $_GET['action'] == 'update_product' || $_GET['action'] == 'insert_product')
		redirect(href_link(FILENAME_CATEGORIES, 'cPath='.$_GET['cPath'].'&'.get_all_get_params(array ('cPath', 'action', 'cID', 'menu'))));
}

if(is_dir(DIR_FS_CATALOG_IMAGES)) {
	if (!is_writeable(DIR_FS_CATALOG_IMAGES))
		$message_stack->add(ERROR_CATALOG_IMAGE_DIRECTORY_NOT_WRITEABLE, 'error');
} else
	$message_stack->add(ERROR_CATALOG_IMAGE_DIRECTORY_DOES_NOT_EXIST, 'error');

require_once(DIR_WS_INCLUDES.'/xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();

if($_GET['action'] == 'new_product') {
	require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.additional_text.php');
	require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.attribut_manager.php');
	require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.google.php');
	require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.products_filter.php');
	require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.products_linking.php');
	require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.products_images.php');
	require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.tag_words.php');
	require(DIR_FS_ADMIN.'group_prices.server.php');
} else
	require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.categories.php');

require(DIR_FS_ADMIN.DIR_WS_INCLUDES.'xajax/xajax.misc.php');
$xajax->processRequest();
include(DIR_WS_INCLUDES.'metatag.php');
$xajax->printJavascript();
?>
</head>
<body>
	<?php require(DIR_WS_INCLUDES.'header.php'); ?>
	<div id="wrapper">
		<table class="outerTable" cellpadding="0" cellspacing="0">
			<tr>
				<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
					<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
				</td>
				<td class="boxCenter" valign="top">
					<table width="100%" cellspacing="0" cellpadding="0">
						<?php
							if ($_GET['action'] == 'new_category' || $_GET['action'] == 'edit_category')
								include (DIR_WS_MODULES.'new_category.php');
							elseif ($_GET['action'] == 'new_product')
								include (DIR_WS_MODULES.'new_product.php');
							elseif ($_GET['action'] == 'edit_crossselling')
								include (DIR_WS_MODULES.'cross_selling.php');
							else {
								if(!$cPath)
									$cPath = '0';
								include (DIR_WS_MODULES.'categories_view.php');
							}
						?>
					</table>
				</td>
			</tr>
		</table>
	</div>
	<div id="show_overlay"></div>
    <?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>